package com.iwhalecloud.bss.kite.cucc.listener.handler.detail.impl;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneProdInstService;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts2;
import com.iwhalecloud.bss.kite.common.unicode.UnicodeRoleKite;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.cache.ProdFeeCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Offer;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.OfferDetail;
import com.iwhalecloud.bss.kite.listener.handler.detail.impl.ProdDetailHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQueryInstTemplateService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.dto.KiteInstDTO;
import com.iwhalecloud.bss.kite.manager.dto.KiteInstRelDTO;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.enums.RelType;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.graph.IRelType;
import com.iwhalecloud.bss.kite.manager.inst.graph.impl.RelEdge;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.iwhalecloud.bss.kite.service.scene.bo.OperateSceneProdInstBO;
import com.iwhalecloud.bss.kite.service.util.OfferUtils;
import com.ztesoft.bss.base.staticdata.CoreDataOffer;
import com.ztesoft.bss.svcability.util.ListUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Primary
public class CuccProdDetailHandler extends ProdDetailHandler {

    @Autowired
    IOperateSceneProdInstService prodInstService;

    @Autowired
    IOperateSceneInstService sceneInstOperateService;

    @Autowired
    IQuerySceneInstService sceneInstQueryService;

    @Autowired
    IQueryInstTemplateService queryInstTemplateService;

    @Autowired
    OperateSceneProdInstBO operateSceneProdInstBO;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    private static final EnumSet<ActionType> ACTION_TYPE_AMK = EnumSet.of(ActionType.A, ActionType.M, ActionType.K);

    /**
     * 根据父对象获取约束
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<OfferDetail> initRestrict(String parentSpecId) {
        return OfferCache.get(parentSpecId).listAccessProdDetail();
    }

    /**
     * 新装的情况下
     */
    @Override
    public void init(String sceneInstId, String parentSpecId, String parentInstId, String custId, String staffId) {
        init(sceneInstId, parentSpecId, parentInstId, null, custId, staffId, null);
    }

    /**
     * 新装初始化
     * @param sceneInstId
     * @param parentSpecId
     * @param parentInstId
     * @param custId
     * @param staffId
     * @param detailIds 相同角色选中产品明细
     */
    public void init(String sceneInstId, String parentSpecId, String parentInstId, KiteInstRelDTO offerObjInstRel, String custId, String staffId, List<String> detailIds) {
        // 取约束
        List<OfferDetail> details = this.initRestrict(parentSpecId);
        Offer offer = OfferCache.get(parentSpecId);

        filterOfferDetail(detailIds, details);

        KiteOfferInst mainOfferInst = null;
        Set<String> applicableRoles = null;

        if (StringUtils.equals(offer.getOfferType(), CoreDataOffer.OFF_0005_12)) {
            /**
             * 处理共享流量包，根据成员明细添加最小值
             */
            KiteOfferInst promotionOfferInst = sceneInstQueryService.getPromotionOfferInstForAdditionalOfferInstId(custId, parentInstId);
            if (!Objects.isNull(promotionOfferInst)) {
                Offer promotionOffer = OfferCache.get(promotionOfferInst.getOfferId());
                //通过可选包取促销实例
                if (KeyConsts.OFFER_SYS_TYPE_1599.equals(promotionOffer.getOfferSysType())) {
                    //促销下的明细。
                    List<KiteInstDTO> kiteInstDtos = sceneInstQueryService.listOfferInstDetails(custId, promotionOfferInst.getOfferInstId());
                    //将促销下的明细构成跟可选包的规格进行配置对比，返回details，进行添加关系
                    details =  dealAdditionTrafficProdInst(sceneInstId, parentInstId,
                        kiteInstDtos, details, promotionOfferInst.getOfferInstId(), custId);
                }
            }

            // 可选包适用构成
            mainOfferInst = sceneInstQueryService.getParentOfferInstByOfferInstId(custId, parentInstId);
            applicableRoles = OfferUtils.getOfferApplicableRoles(mainOfferInst.getOfferId(), parentSpecId);
        }

        // 构建最小个数成员
        for (OfferDetail detail : details) {
            int minNum = NumberUtils.toInt(detail.getMinNum(), 0);
            int maxNum = NumberUtils.toInt(detail.getMaxNum(), 0);
            if (maxNum == 0) {
                continue;
            }
            // 判断最小构成
            while (minNum-- > 0) {
                if (KeyConsts.OFFER_TYPE_15.equals(offer.getOfferType())) {
                    // 增加明细
                    operateSceneProdInstBO
                        .initProdInstForOffer(sceneInstId, parentInstId, parentSpecId, detail.getObjId(), detail.getRoleId(),
                            custId, staffId, detail.getDetailId());
                }
                // 判断是否存在套餐的明细，如果存在，只需要增加关系；如果不存在，需要增加实例的同时增加关系
                else if (CoreDataOffer.OFF_0005_13.equals(offer.getOfferType())) {
                    // FIXME 需要优化
                    // 取套餐下的相同角色下的明细
                    KiteProdInst prodInst = this.getDetailProductNotExists(sceneInstId, detail, custId, parentInstId);
                    if (prodInst != null) {
                        sceneInstOperateService.addOfferObjInstRel(sceneInstId, InstType.PROMOTION_OFFER_INST, parentInstId,
                            prodInst.getInstType(), prodInst.getProdInstId(), RelType.OFFER_OBJ_REL_TYPE_1000,
                            ActionType.A, detail.getRoleId(), detail.getDetailId(), prodInst.getOwnerCustId());
                    }
                    else {
                        // 增加明细
                        operateSceneProdInstBO
                            .initProdInstForOffer(sceneInstId, parentInstId, parentSpecId, detail.getObjId(), detail.getRoleId(),
                                custId, staffId, detail.getDetailId());
                    }
                }
                else if (StringUtils.equals(offer.getOfferType(), CoreDataOffer.OFF_0005_12)) {
                    // 1697449 判断可选包成员不一致
                    if (null != offerObjInstRel && !detail.getRoleId().equals(offerObjInstRel.getRoleId()) && !applicableRoles.contains(offerObjInstRel.getRoleId())) {
                        continue;
                    }

                    Set<String> set = applicableRoles;
                    List<KiteProdInst> prodInsts = sceneInstQueryService.listProdInstsFromOfferInst(custId, mainOfferInst.getInstId()).stream()
                        .filter(t -> t.getActionType() != ActionType.D)
                        .filter(t -> Objects.equals(t.getProdId(), detail.getObjId()))
                        .filter(t -> Objects.equals(t.getRoleId(), detail.getRoleId()) || set.contains(t.getRoleId()))
                        .collect(Collectors.toList());

                    if (!ListUtil.isEmpty(prodInsts)) {
                        for (KiteProdInst prodInst : prodInsts) {
                            sceneInstOperateService.addOfferObjInstRel(sceneInstId, InstType.ADDITIONAL_OFFER_INST,
                                parentInstId, prodInst.getInstType(), prodInst.getProdInstId(),
                                RelType.OFFER_OBJ_REL_TYPE_1000, ActionType.A, detail.getRoleId(),
                                detail.getDetailId(), prodInst.getOwnerCustId());
                        }
                    }
                }
                else {
                    //主套餐
                    operateSceneProdInstBO
                        .initProdInstForOffer(sceneInstId, parentInstId, parentSpecId, detail.getObjId(), detail.getRoleId(), custId,
                            staffId, detail.getDetailId());
                }
            }
        }
    }

    /**
     * 过滤相同角色的明细
     * @param detailIds 同角色选中产品明细
     * @param details 明细
     */
    private void filterOfferDetail(List<String> detailIds, List<OfferDetail> details) {
        if (KiteListUtils.isNotEmpty(detailIds)) {
            detailIds.forEach(detailId -> {
                List<OfferDetail> curOfferDetail = details.stream()
                    .filter(offerDetail -> offerDetail.getDetailId().equals(detailId)).collect(Collectors.toList());
                if (KiteListUtils.isNotEmpty(curOfferDetail)) {
                    details.removeIf(
                        offerDetail -> offerDetail.getRoleId().equals(curOfferDetail.get(0).getRoleId()) && !offerDetail
                            .getObjId().equals(curOfferDetail.get(0).getObjId()));
                }

            });
        }
    }



    /**
     * 可选包明细关系处理
     * @param sceneInstId
     * @param additionOfferId
     * @param parentInstId
     * @param additionalOfferInstId
     * @param custId
     * @param staffId
     */
    public void add(String sceneInstId, String additionOfferId, String parentInstId, String additionalOfferInstId, String prodInstId, String custId, String staffId) {
        addAdditionalOfferDetail(sceneInstId, additionOfferId, parentInstId, additionalOfferInstId, prodInstId, custId, staffId, false);
    }

    /**
     * 可选包明细关系处理
     * @param sceneInstId
     * @param additionOfferId
     * @param parentInstId
     * @param additionalOfferInstId
     * @param custId
     * @param staffId
     */
    public void addAdditionalOfferDetail(String sceneInstId, String additionOfferId, String parentInstId, String additionalOfferInstId, String prodInstId, String custId, String staffId, boolean isExtOffer) {
        // 取约束
//        List<OfferDetail> details = this.initRestrict(additionOfferId);

        Offer offer = ProdFeeCache.get(additionOfferId);
     // 取约束
        List<OfferDetail> details = offer.listAccessProdDetail();

        KiteProdInst currentProdInst = sceneInstQueryService.getProdInst(custId, prodInstId);

        for (OfferDetail detail : details) {
            // 判断是否存在套餐的明细，如果存在，只需要增加关系；如果不存在，需要增加实例的同时增加关系
            if (CoreDataOffer.OFF_0005_13.equals(offer.getOfferType())) {
                // 取套餐下的相同角色下的明细
                KiteProdInst prodInst = this.getDetailProductNotExists(sceneInstId, detail, custId, parentInstId);
                if (prodInst != null) {
                    sceneInstOperateService.addOfferObjInstRel(sceneInstId, InstType.PROMOTION_OFFER_INST, additionalOfferInstId,
                        prodInst.getInstType(), prodInst.getProdInstId(), RelType.OFFER_OBJ_REL_TYPE_1000,
                        ActionType.A, detail.getRoleId(), detail.getDetailId(), prodInst.getOwnerCustId());
                    //                        .addInstRel(sceneInstId, InstType.PROMOTION_OFFER_INST, additionalOfferInstId, prodInst.getInstType(),
                    //                            prodInst.getProdInstId(), RelType.OFFER_OBJ_REL_TYPE_1000, ActionType.A);
                }
                else {
                    // 增加明细
                    operateSceneProdInstBO.initProdInstForOffer(sceneInstId, parentInstId, detail.getObjId(),
                        detail.getRoleId(), custId, staffId, detail.getDetailId());
                }
            }
            else if (StringUtils.equals(offer.getOfferType(), CoreDataOffer.OFF_0005_12)) {

                //增加适用构成适配
                RelEdge offerObjInstRel = sceneInstQueryService.getInstRel(custId, InstType.MAIN_OFFER_INST, parentInstId, InstType.PROD_INST, prodInstId, RelType.OFFER_OBJ_REL_TYPE_1000);

                List<KiteProdInst> prodInsts = null;

                if (!Objects.isNull(offerObjInstRel)) {
                    //产品实例是挂在主套餐下的
                    KiteOfferInst mainOfferInst = sceneInstQueryService.getParentOfferInstByOfferInstId(custId, additionalOfferInstId);
                    Set<String> applicableRoles = OfferUtils.getOfferApplicableRoles(mainOfferInst.getOfferId(), additionOfferId);
                    prodInsts = sceneInstQueryService.listProdInstsFromOfferInst(custId, mainOfferInst.getInstId()).stream()
                        .filter(t -> t.getActionType() != ActionType.D)
                        .filter(t -> Objects.equals(t.getProdId(), detail.getObjId()))
                        .filter(t -> Objects.equals(t.getRoleId(), detail.getRoleId()) || applicableRoles.contains(t.getRoleId()))
                        .collect(Collectors.toList());
                    List<KiteProdInst> sceneProdInsts = sceneInstQueryService.listProdInsts(sceneInstId);
                    for (KiteProdInst kiteProdInst : sceneProdInsts) {
                        if (KiteStringUtils.equals(kiteProdInst.getProdId(), detail.getObjId()) && KiteStringUtils.equals(detail.getRoleId(), kiteProdInst.getRoleId())) {
                            prodInsts.add(kiteProdInst);
                        }
                    }
                }
                else {
                    KiteOfferInst offerInst = sceneInstQueryService.getOfferInst(custId, parentInstId);
                    if (InstType.PROMOTION_OFFER_INST == offerInst.getInstType() && KeyConsts.OFFER_SYS_TYPE_1599.equals(OfferCache.get(offerInst.getOfferId()).getOfferSysType())) {
                        KiteOfferInst mainOfferInst = sceneInstQueryService.getMainOfferInstByProdInstId(custId, prodInstId);
                        if (mainOfferInst != null) {
                            prodInsts = sceneInstQueryService.listProdInstsFromOfferInst(custId, mainOfferInst.getOfferInstId());
                        }
                    }
                    else {
                        //加装可选包只建自己的关系
                        if (isExtOffer && StringUtils.equals(detail.getObjId(), currentProdInst.getProdId())) {
                            prodInsts = Lists.newArrayList(currentProdInst);
                        }
                        else {
                            //查找通过可选包加装的产品
                            //1、主套餐、促销
                            prodInsts = sceneInstQueryService.listProdInstsByProdId(sceneInstId, detail.getRoleId(), detail.getObjId());
                            /**主套餐、促销需要将场景中的所有满足条件的成员与可选包建立关系,针对父子合约、共享流量等销售品，加装成员的时候，
                             * 只需将当前加装角色所在的主套餐有相同成员的情况建立关系，此处增加判断**/
                            //2、父子合约、共享流量等销售品 offer_sys_name＝1599
                            List<KiteOfferInst> kiteOfferInsts = sceneInstQueryService.listPromotionOfferInsts(sceneInstId);
                            if (!Objects.isNull(kiteOfferInsts)) {
                                for (KiteOfferInst mOfferInst : kiteOfferInsts) {
                                    Offer promotionOffer = OfferCache.get(mOfferInst.getOfferId());
                                    //通过可选包取促销实例
                                    if (KeyConsts.OFFER_SYS_TYPE_1599.equals(promotionOffer.getOfferSysType())) {
                                        prodInsts = sceneInstQueryService.listProdInstsFromOfferInst(custId, parentInstId);
                                    }
                                }
                            }
                        }
                    }
                }

                if (!ListUtil.isEmpty(prodInsts)) {
                    // 让传入的产品实例具有最高优先级
                    if (!Objects.equals(prodInsts.get(0).getProdInstId(), prodInstId)) {
                        for (int i = 1; i < prodInsts.size(); i++) {
                            if (Objects.equals(prodInsts.get(i).getProdInstId(), prodInstId)) {
                                prodInsts.set(0, prodInsts.set(i, prodInsts.get(0)));
                                break;
                            }
                        }
                    }
                    //现场可选包的成员在界面上没有位置，所以可选包的明细条数据按最大值默认；
                    int maxNum = NumberUtils.toInt(detail.getMaxNum(), 1);
                    for (int i = 0; i < prodInsts.size() && i < maxNum; i++) {
                        KiteProdInst prodInst = prodInsts.get(i);
                        //仅对动作类型为A/M/K且没有未有订购可选包的实例添加可选包明细
                        if (!ACTION_TYPE_AMK.contains(prodInst.getActionType()) || !Objects.equals(prodInst.getProdId(), detail.getObjId())) {
                            continue;
                        }
                        List<KiteInstRelDTO> instRels = sceneInstQueryService.listInstRelsByZInst(custId, InstType.PROD_INST, prodInst.getProdInstId(), InstType.ADDITIONAL_OFFER_INST);
                        boolean exists = instRels.stream().map(KiteInstRelDTO::getaInstId).anyMatch(t -> t.equals(additionalOfferInstId));
                        if (!exists) {
                            if (KeyConsts.CDMATYSJ_PRODUCT_ID.equals(prodInst.getProdId()) && KeyConsts2.MASTER_SECONDARY_CARD_ROLE.equals(detail.getRoleId())) {
                                detail.setRoleId(prodInst.getRoleId());
                            }
                            sceneInstOperateService.addOfferObjInstRel(sceneInstId, InstType.ADDITIONAL_OFFER_INST, additionalOfferInstId,
                                prodInst.getInstType(), prodInst.getProdInstId(), RelType.OFFER_OBJ_REL_TYPE_1000, ActionType.A,
                                detail.getRoleId(), detail.getDetailId(), prodInst.getOwnerCustId());
                            //                            sceneInstOperateService.addInstRel(sceneInstId, InstType.ADDITIONAL_OFFER_INST, additionalOfferInstId,
                            //                                prodInst.getInstType(), prodInst.getProdInstId(),
                            //                                RelType.OFFER_OBJ_REL_TYPE_1000, ActionType.A, detail.getRoleId());
                        }
                    }
                }
            }
        }
    }

    /**
     * 找同角色下，套餐上的明细
     *
     * @param mainDetailInsts
     * @param custId
     * @param parentInstId
     * @param roleId
     * @return
     */
    private KiteProdInst getDetailsNotExists(List<KiteProdInst> mainDetailInsts, String custId, String parentInstId, String roleId) {
        // 当前可选包的明细列表
        List<KiteProdInst> prodInsts = sceneInstQueryService.listProdInstsFromOfferInstByRoleId(custId, parentInstId, roleId);
        // 找到套餐上相同角色的明细，还没有加入到可选包里面的明细
        for (KiteProdInst mainDetailInst : mainDetailInsts) {
            boolean exists = false;
            for (KiteProdInst detailInst : prodInsts) {
                if (mainDetailInst.getInstId().equals(detailInst.getInstId())) {
                    exists = true;
                    break;
                }
            }
            if (!exists) {
                return mainDetailInst;
            }
        }
        return null;
    }

    @Override
    public boolean check(String sceneInstId, String specId, String instId) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void apply() {
        // TODO Auto-generated method stub

    }

    @Override
    public void remove(String sceneInstId, String specId, String instId) {
        List<KiteProdInst> detailInsts = sceneInstQueryService.listProdInstsFromOfferInst(sceneInstId, instId); // 通过标准服务查询当前删除的套餐下的成员
        detailInsts.forEach(detailInst -> {
            // 调标准服务，删除该销售品的明细
            prodInstService.removeProdInst(sceneInstId, detailInst.getInstId());
        });
    }

    /***
     * 在套餐里面查询是否有匹配的产品实例
     * @param sceneInstId
     * @param detail
     * @param custId
     * @param offerInstId
     * @return
     */
    private KiteProdInst getDetailProductNotExists(String sceneInstId, OfferDetail detail, String custId, String offerInstId) {

        List<KiteOfferInst> mainOfferInsts = sceneInstQueryService.listMainOfferInsts(sceneInstId);
        KiteProdInst prodInst = null;
        for (KiteOfferInst mainOfferInst : mainOfferInsts) {

            // 套餐下同角色的成员列表
            List<KiteProdInst> mainDetailInsts = sceneInstQueryService.listProdInstsFromOfferInstByRoleId(custId, mainOfferInst.getInstId(), detail.getRoleId());
            // 不限主副卡角色
            if (UnicodeRoleKite.UNLIMITED_ROLE.equals(detail.getRoleId())) {
                mainDetailInsts = sceneInstQueryService.listProdInstsFromOfferInstByProdId(custId, mainOfferInst.getInstId(), detail.getObjId());
            }

            // 取套餐下的相同角色下的明细
            prodInst = this.getDetailsNotExists(mainDetailInsts, custId, offerInstId, detail.getRoleId());


            if (Objects.isNull(prodInst)) {

                /** 特殊场景：[1743729]【专题测试】【打包受理】第二条宽带场景改造 ；通过明细找到这种比较特殊的促销；
                 *  特殊判断:
                 *      1、判断当前销售品实例最上层是否有礼包销售品；
                 *      2、判断明细上是否有第二条宽带的明细；
                 */
                prodInst = this.getPecialProdInst(mainOfferInst, detail, custId);
            }
            else if (!Objects.isNull(prodInst) && isPecial(offerInstId, detail.getOfferId(), custId)) {
                return null;
            }


            if (prodInst != null) {
                return prodInst;
            }
        }
        return prodInst;
    }

    /***
     * 在套餐实例中找特殊场景的明细
     * @param mainOfferInst 主套餐实例Id
     * @param detail 促销明细
     * @param custId 客户Id
     * @return
     */
    private KiteProdInst getPecialProdInst(KiteOfferInst mainOfferInst, OfferDetail detail, String custId) {
        KiteOfferInst giftOfferInst = sceneInstQueryService.getGiftOfferInstByOfferInstId(custId, mainOfferInst.getOfferInstId());
        /***
         * 特殊场景：[1743729]【专题测试】【打包受理】第二条宽带场景改造 ；通过明细找到这种比较特殊的促销；
         *   特殊判断:
         *      1、判断当前销售品实例最上层是否有礼包销售品；
         *      2、判断明细上是否有第二条宽带的明细；
         */
        if (!Objects.isNull(giftOfferInst) && KiteStringUtils.equals(detail.getRoleId(), "78000002")) {
            // 如果存在这种关系，就判定为第二条宽带场景改造特殊处理
            List<KiteProdInst> prodInsts = sceneInstQueryService.listProdInstsFromOfferInst(custId, mainOfferInst.getOfferInstId());
            for (KiteProdInst prodInst : prodInsts) {
                RelEdge relEdge = sceneInstQueryService.getInstRel(custId, InstType.MAIN_OFFER_INST, mainOfferInst.getOfferInstId(), InstType.PROD_INST, prodInst.getProdInstId(), RelType.OFFER_OBJ_REL_TYPE_1000);
                if (KiteStringUtils.equals("78000002", detail.getRoleId()) && KiteStringUtils.equals("70000002", relEdge.getRoleId())) {
                    // 返回角色为 70000002 的产品实例；
                    return prodInst;
                }
            }
        }
        return null;
    }

    private boolean isPecial(String promotionOfferInstId, String offerId, String custId) {

        KiteOfferInst giftOfferInst = sceneInstQueryService.getGiftOfferInstByOfferInstId(custId, promotionOfferInstId);
        Offer offer = OfferCache.get(offerId);
        List<OfferDetail> offerDetails = offer.listAccessProdDetail();
        boolean hasFlag = offerDetails.stream().anyMatch(offerDetail -> KiteStringUtils.equals("78000002", offerDetail.getRoleId()));
        return hasFlag && !Objects.isNull(giftOfferInst);
    }

    /***
     * 在套餐里面查询是否有匹配的产品实例
     * @param sceneInstId
     * @param roleId
     * @param custId
     * @param offerInstId
     * @return
     */
    private List<KiteProdInst> getMainOfferDetailProductNotExists(String sceneInstId, String roleId, String custId,
                                                                  String offerInstId) {

        // KiteOfferInst mainOfferInst = sceneInstQueryService.getMainOfferInstByAdditionalOfferInstId(custId,
        // offerInstId);
        KiteOfferInst mainOfferInst = sceneInstQueryService.getParentOfferInstByOfferInstId(custId, offerInstId);

        // 套餐下同角色的成员列表
        List<KiteProdInst> mainDetailInsts = sceneInstQueryService.listProdInstsFromOfferInstByRoleId(custId,
            mainOfferInst.getInstId(), roleId);

        return mainDetailInsts;
    }

    @Override
    public void load(String sceneInstId, String specId, String instId, String custId, String staffId) {
        // TODO Auto-generated method stub

    }

    private  List<OfferDetail> dealAdditionTrafficProdInst(String sceneInstId, String parentInstId, List<KiteInstDTO> kiteInstDtos,  List<OfferDetail> details,
                                                           String promotionOfferInstId, String custId) {
        if (KiteListUtils.isEmpty(kiteInstDtos) || KiteListUtils.isEmpty(details)) {
            return details;
        }
        int zmin = 0;
        int fmin = 0;
        for (KiteInstDTO dto :kiteInstDtos) {
            if (dto.getInstId().equals(promotionOfferInstId)) {
                Set<RelEdge> subEdges = dto.getSubEdges();
                for (RelEdge edge:subEdges) {
                    for (OfferDetail  detail : details) {
                        InstType targetInstType = edge.getTargetInstType();
                        String targetInstId = edge.getTargetInstId();

                        String targetRole = edge.getRoleId();
                        IRelType targetRelType = edge.getRelType();
                        if (targetInstType.equals(InstType.PROD_INST) && Objects.equals(RelType.OFFER_OBJ_REL_TYPE_1000, targetRelType)) {
                            KiteProdInst prodInst = sceneInstQueryService.getProdInst(custId, targetInstId);
                            if (prodInst.getProdId().equals(detail.getObjId()) && detail.getRoleId().equals(targetRole)
                                && "100000".equals(detail.getObjType())) {
                                if (detail.getRoleId().equals("39999")) {
                                    zmin += 1;
                                    detail.setMinNum(zmin + "");
                                }
                                else if (detail.getRoleId().equals("40000")) {
                                    fmin += 1;
                                    detail.setMinNum(fmin + "");
                                }
                            }
                        }
                    }
                }
            }
        }
        return details;
    }
}
